<template>
  <el-card class="mt-10">
    <el-button
      type="primary"
      :icon="Plus"
      :disabled="!categoryStore.category3Id"
      @click="showAddSpu"
    >
      添加SPU
    </el-button>
    <el-table :data="spuList" border class="mt-20">
      <el-table-column type="index" label="序号" width="60" align="center" />
      <el-table-column label="SPU名称" prop="spuName" />
      <el-table-column label="SPU描述" prop="description" />
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-tooltip effect="light" content="添加SKU" placement="bottom">
            <el-button
              type="primary"
              :icon="Plus"
              size="small"
              @click="showAddSku(row)"
            />
          </el-tooltip>
          <el-tooltip effect="light" content="修改SPU" placement="bottom">
            <el-button
              type="warning"
              :icon="Edit"
              size="small"
              @click="showUpdateSpu(row)"
            />
          </el-tooltip>
          <el-tooltip effect="light" content="查看SKU列表" placement="bottom">
            <el-button type="info" :icon="InfoFilled" size="small" />
          </el-tooltip>
          <el-tooltip effect="light" content="删除SPU" placement="bottom">
            <el-button type="danger" :icon="Delete" size="small" />
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      class="mt-20"
      v-model:currentPage="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[3, 6, 9, 12]"
      layout="prev, pager, next, jumper, ->, sizes, total"
      :total="total"
      @size-change="getSpuList"
      @current-change="getSpuList"
    />
  </el-card>
</template>

<script lang="ts">
export default {
  name: "SpuList",
};
</script>

<script lang="ts" setup>
import { ref, watch } from "vue";
import { Plus, Edit, InfoFilled, Delete } from "@element-plus/icons-vue";
import { getSpuListApi } from "@/api/product/spu";
import type { SpuList, SpuItem } from "@/api/product/model/spuModel";
import { useCategoryStore } from "@/stores/category";
import { useSpuStore } from "@/stores/spu";

const emit = defineEmits(["setIsShow"]);

const spuStore = useSpuStore();

// 显示添加SPU
const showAddSpu = () => {
  spuStore.$reset();
  emit("setIsShow", 2);
};

const showUpdateSpu = (row: SpuItem) => {
  const { id, spuName, tmId, description } = row;
  spuStore.$patch({
    id,
    spuName,
    tmId,
    description,
  });
  emit("setIsShow", 2);
};

const showAddSku = (row: SpuItem) => {
  const { id, spuName } = row;
  spuStore.$patch({
    id,
    spuName,
  });
  emit("setIsShow", 3);
};

const currentPage = ref(1);
const pageSize = ref(3);
const total = ref(0);

const spuList = ref<SpuList>([]);

const getSpuList = async () => {
  const res = await getSpuListApi({
    page: currentPage.value,
    limit: pageSize.value,
    category3Id: categoryStore.category3Id as number,
  });

  spuList.value = res.records;
  total.value = res.total;
};

const categoryStore = useCategoryStore();

watch(
  () => categoryStore.category3Id,
  (category3Id) => {
    if (!category3Id) {
      spuList.value = [];
      currentPage.value = 1;
      pageSize.value = 3;
      total.value = 0;
      return;
    }
    getSpuList();
  },
  { immediate: true }
);
</script>

<style scoped></style>
